package com.example.eduservice.controller;


import com.example.commonutils.R;
import com.example.eduservice.client.VodClient;
import com.example.eduservice.entity.EduChapter;
import com.example.eduservice.entity.EduVideo;
import com.example.eduservice.entity.chapter.VideoVo;
import com.example.eduservice.service.EduVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-08-30
 */
@RestController
@RequestMapping("/eduservice/video")
//@CrossOrigin
public class EduVideoController {

    @Autowired
    private EduVideoService VideoService;

    @Autowired
    private VodClient vodClient;

    //添加小节
    @PostMapping("/addVideo")
    public R addVideo(@RequestBody EduVideo eduVideo){
        VideoService.save(eduVideo);
        System.out.println("------di--"+eduVideo);
        return R.ok();
    }

    //删除小节，删除对应阿里云视频
    @DeleteMapping("{id}")
    public R deleteVideo(@PathVariable String id){
        //根据小节id获取视频id，调用方法实现视频删除
        EduVideo eduVideo=VideoService.getById(id);
        String videoSourceId = eduVideo.getVideoSourceId();

        if(!StringUtils.isEmpty(videoSourceId)){
            //本地接口远程调用别的微服务，实现删除视频
            vodClient.removeAlyVideo(videoSourceId);
        }
        //删除小节
        VideoService.removeById(id);
        return R.ok();
    }

    //修改章节
    @PostMapping("updateVideo")
    public R updateChapter(@RequestBody  EduVideo eduVideo){
        VideoService.updateById(eduVideo);
        return R.ok();
    }
    //根据章节id查询
    @GetMapping("getVideo/{videoId}")
    public R getChapter(@PathVariable String videoId){
        EduVideo eduVideo = VideoService.getById(videoId);
        return R.ok().data("video",eduVideo);
    }

}

